package activity

import android.content.Intent
import android.widget.Toast
import androidx.activity.viewModels
import com.blankj.utilcode.util.ImageUtils
import com.blankj.utilcode.util.ToastUtils
import com.blankj.utilcode.util.UriUtils
import com.wd.common.base.BaseActivity
import com.wd.common.viewmodel.MyViewModel
import com.wd.rim.databinding.ActivityReleaseBinding
import java.io.File

class ReleaseActivity : BaseActivity<ActivityReleaseBinding, Any?>() {
    var file: File? =null
    val list= mutableListOf<File>()
        private val mviewmodel:MyViewModel by viewModels()
    override fun getViewBinding()=ActivityReleaseBinding.inflate(layoutInflater)

    override fun initData() {
      binding.zhuan.setOnClickListener {
          val intent = Intent()
          intent.setClass(this,ZhuanActivity::class.java)
          startActivity(intent)
      }

        binding.imageId.setOnClickListener {
            if(file==null){
                startActivityForResult(Intent(Intent.ACTION_PICK).setType("image/*"),1)
            }else{

            }
        }
        binding.send.setOnClickListener {
           val title = binding.etTitle.text.toString()
            val Comment = binding.etComment.text.toString()
            if(title!=null&&Comment!=null){
                mviewmodel.getSendRim(
                   title,
                    1,
                    1,
                    Comment,
                    "北京",
                    list

                )
                finish()
            }else{
                Toast.makeText(this,"请表达你的态度",Toast.LENGTH_SHORT).show()
            }


        }

        mviewmodel.SendRim.observe(this){
            Toast.makeText(this,it.message,Toast.LENGTH_SHORT).show()
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==1&&data!=null){
            val data1 = data.data
            file=UriUtils.uri2File(data1)
            list.add(file!!)
            val bitmap = ImageUtils.getBitmap(file)
            binding.imageId.setImageBitmap(bitmap)
        }
    }


}